<?php
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */


/**
* @package DomAr
* @subpackage DomArHistory
*/
class DomArHistory extends DomArSet {
	
	/**
	 * @var DomArHistoryItem
	 */
	private $historyItem;
	/**
	 * Returns current history version.
	 * 
	 * Each time this method is called a query is sent to database.
	 * 
	 * @return Int
	 */
	public function getLastVersionNr( ) {
		$v = $this->dataQuery()->flushFields()->select('MAX(t.version) as version')->limit(1)->commit();
		return (int)$v['version'];
	}
	/*
	 * @param Int $versionNr
	 * @return DomArHistoryItem
	 */
	public function getVersion( $versionNr = NULL ) {
		return $this->query()->where('t.version = '.(int)$versionNr)->limit(1)->commit();
	}
	/**
	 * Returns last version item
	 * @return DomArHistoryItem
	 */
	public function getLastVersion() {
		return $this->query()->orderBy('t.version DESC')->limit(1)->commit();
	}
	/**
	 * Registeres object property value change. 
	 *
	 * @param String $propertyName
	 */
	public function registerChange( $propertyName ) {
		if( !$this->getParent()->id )
			return;
		
		if( !$this->historyItem )
			$this->prepareHistoryItem();
		
		$this->historyItem->addChangedPropertyName( $propertyName );
	}
	/**
	 * Prepares history item that has current parent object property values.
	 */
	public function prepareHistoryItem() {
		$historyItemClassName = $this->getProperty()->childClassName;
		$h = new $historyItemClassName;
		
		foreach( $this->getProperty()->properties as $propertyName => $true) {
			$h->$propertyName = $this->getParent()->$propertyName;
		}
		$h->version = $this->getLastVersionNr()+1;
		$this->historyItem = $h;
		$this[] = $h;
	}
	/**
	 * Removes current history item.
	 */
	public function resetHistoryItem() {
		$this->historyItem = NULL;
	}
	/**
	 * Restores parent object from history.
	 *
	 * @param Int $versionNr
	 * @return Bool true on success, false on failure.
	 */
	public function restoreFrom( $versionNr ) {
		
		$historyItem = $this->query()->where('t.version = '.(int)$versionNr)->limit(1)->commit();
		if( !$historyItem )
			return false;
			
		foreach( $this->getProperty()->properties as $propertyName => $true) {
			$restoreMethodName = 'get'.ucfirst($propertyName).'RestoreValue';
			if( method_exists($historyItem, $restoreMethodName ) ) 
				$this->getParent()->$propertyName = call_user_func_array( array($historyItem, $restoreMethodName), array());
			else
				$this->getParent()->$propertyName = $historyItem->$propertyName;
		}
		return true;
	}
	
}
?>